/**
 * 
 */
package std.game.behaviours

import java.util.Set;


import org.groovymud.object.MudObject
import org.groovymud.object.Container
import org.groovymud.object.registry.InventoryHandler
import org.groovymud.object.registry.MudObjectAttendant;
import org.groovymud.object.registry.Registry
import org.groovymud.shell.command.CommandInterpreter;
import org.easymock.classextension.MockClassControl
import org.easymock.MockControl
import std.game.objects.containers.ContainerImpl
import std.game.objects.GroovyMudObject

/**
 * @author corbym
 *
 */
class SpawnBehaviourTest extends GroovyTestCase{
	
	/* (non-Javadoc)
	 * @see std.game.behaviours.SpawnBehaviour#doBehaviour(org.groovymud.object.MudObject)
	 */
	void testDoBehaviour(){
		def obj = { getName : {"test" } } as GroovyMudObject
		Behaviour spawnBehaviour = new SpawnBehaviour(spawnItems : [(SpawnChecks.CHECK_IN_ROOM) : obj])
		
		
		def mockRegistry = new Registry(null){
					public Set<MudObject> getMudObjects(String name) {
						
					};
				}
		
		def mockHandler = new InventoryHandler(){
					public Registry getRegistry() {
						return mockRegistry;
					}
				}
		boolean added = false;
		def mockContainer = new ContainerImpl(){
					public InventoryHandler getInventoryHandler() {mockHandler
					}
					public Set getMudObjects() {null
					};
					public void addMudObject(MudObject object) {
						added=true
						assert obj == object
					};
				};	
		
		spawnBehaviour.doBehaviour(mockContainer)
		
		
	}
	
	void testDoBehaviour2(){
		def obj = { getName : {"test"
			} } as GroovyMudObject
		Behaviour spawnBehaviour = new SpawnBehaviour(spawnItems : ["checkInRoom" : obj])
		
		MockControl mockRegControl = MockClassControl.createControl(Registry, [InventoryHandler] as Class[], [null] as Object[])
		
		def mockRegistry = mockRegControl.getMock() as Registry
		
		mockRegistry.getMudObjects("test")
		mockRegControl.setDefaultReturnValue([] as Set)
		mockRegControl.replay()
		def mockHandlerCtrl = MockClassControl.createNiceControl(InventoryHandler)
		
		def mockHandler = mockHandlerCtrl.getMock() as InventoryHandler
		
		mockHandlerCtrl.replay()
		boolean addedMudObject = false
		def container = [getRegistry : {mockRegistry }, 
		getInventoryHandler : { mockHandler }, getMudObjects : { val -> 
			assert val == "test", "value was not test"
			null
		}, addMudObject : { val -> 
			addedMudObject = true
			assert val == obj, "unexpected value $val, expecting $obj"
		}
		]
		
		spawnBehaviour.doBehaviour(container)
		
		mockHandlerCtrl.verify()
		mockRegControl.verify()
		assert addedMudObject, "mud object was not added"
	}
	
	void testNoSpawnExists(){
		def mockContainer = [:] as Container
		def obj = [getName : {"test"}
		, getCurrentContainer: {mockContainer}] as GroovyMudObject
		
		MockControl regCtrl = MockClassControl.createControl(Registry, [InventoryHandler] as Class[], [null] as Object[])
		
		def mockRegistry = regCtrl.getMock()
		
		mockRegistry.getMudObjects("test")
		regCtrl.setDefaultReturnValue([obj] as Set)
		regCtrl.replay()
		
		def container = [getRegistry:{ mockRegistry },
		addMudObject : { fail("must not add item")}
		] as ContainerImpl
		SpawnBehaviour b = new SpawnBehaviour(spawnItems : [(SpawnChecks.CHECK_EXISTS) : obj])
		
		b.doBehaviour(container)
		regCtrl.verify()
	}
	
	void testNoSpawnPresent(){
		def obj = [getName : {"test"}] as MudObject
		def mockHandler = [:] as InventoryHandler
		def container = [getInventoryHandler: {mockHandler},
		addMudObject : { fail("must not add item")},
		getMudObjects: {name ->
			assertEquals(obj.getName(), name) 
			return [obj] as Set
		}
		] as ContainerImpl
		SpawnBehaviour b = new SpawnBehaviour(spawnItems : [(SpawnChecks.CHECK_IN_ROOM) : obj])
		
		b.doBehaviour(container)
		
	}
}
